package com.homedo.microservice.open.service.gateway.manager.web.autoconfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${_version}")
    private String _version;
    @Value("${x-springfox.documentation.swagger.v2.basePackage:}")
    private String basePackage;
    @Value("${x-springfox.documentation.swagger.v2.title:}")
    private String title;
    @Value("${x-springfox.documentation.swagger.v2.description:}")
    private String description;
    @Value("${x-springfox.documentation.swagger.v2.contact.name:}")
    private String contactName;
    @Value("${x-springfox.documentation.swagger.v2.contact.url:}")
    private String contactUrl;
    @Value("${x-springfox.documentation.swagger.v2.contact.email:}")
    private String contactEmail;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().contact(new Contact(contactName, contactUrl, contactEmail))
                .title(title)
                .description(description)
                .version(_version)
                .build();
    }

    @Bean
    public UiConfiguration uiConfig() {
        return new UiConfiguration(null, "list", "alpha", "schema",
                UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
    }

}
